#include"tree.h"
void PreOrder(BTNode* root) {
	if (root == NULL) {
		return;
	}
	printf("%d ", root->data);
	PreOrder(root->left);
	PreOrder(root->right);
}
void InOrder(BTNode* root) {
	if (root == NULL) {
		return;
	}
	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}
void PostOrder(BTNode* root) {
	if (root == NULL) {
		return;
	}
	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}
int BinaryTreeSize(BTNode* root) {
	if (root == NULL) {
		return 0;
	}
	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
int BinaryTreeLeafSize(BTNode* root) {
	if (root == NULL) {
		return 0;
	}
	if (root->left == NULL && root->right == NULL) {
		return 1;
	}
	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
int BinaryTreeLevelSize(BTNode* root, int k) {
	if (root == NULL) {
		return 0;
	}
	if (k == 1) {
		return 1;
	}
	return BinaryTreeLevelSize(root->left, k - 1) + BinaryTreeLevelSize(root->right, k - 1);
}
int BinaryTreeDepth(BTNode* root) {
	if (root == NULL) {
		return 0;
	}
	if (root->left == NULL && root->right == NULL) {
		return 1;
	}
	int leftD = BinaryTreeDepth(root->left);
	int rightD = BinaryTreeDepth(root->right);
	return leftD > rightD ? (1 + leftD) : (1 + rightD);
}
BTNode* BinaryTreeFind(BTNode* root,BTDataType val) {
	if (root == NULL) {
		return NULL;
	}
	if (root->data == val) {
		return root;
	}
	BTNode* leftNode = BinaryTreeFind(root->left,val);
	if (leftNode) {
		return leftNode;
	}
	BTNode* rightNode = BinaryTreeFind(root->right,val);
	if (rightNode) {
		return rightNode;
	}
	return 0;
}
void BinaryTreeDestroy(BTNode** root) {
	if ((*root) == NULL) {
		return;
	}
	BinaryTreeDestroy(&(*root)->left);
	BinaryTreeDestroy(&(*root)->right);
	free(*root);
	*root = NULL;
}